package com.xysq.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.CouponModel;
import com.rockcent.model.IndustryGroupModel;
import com.rockcent.model.Topics;
import com.umeng.analytics.MobclickAgent;
import com.xysq.adapter.TopicAndCouponAdapter;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;
import com.xysq.util.UserInfoKeeper;
import com.xysq.widget.LoadData;
import com.xysq.widget.ScrollListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

public class MarketActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

//    @InjectView(R.id.txt_provide_for_free)
//    TextView provideForFreeTxt;
    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;
    @InjectView(R.id.txt_first)
    TextView firstTxt;
    @InjectView(R.id.txt_second)
    TextView secondTxt;
    @InjectView(R.id.txt_third)
    TextView thirdTxt;
    @InjectView(R.id.txt_fourth)
    TextView fourthTxt;
//    @InjectView(R.id.txt_fifth)
//    TextView fifthTxt;
//    @InjectView(R.id.txt_sixth)
//    TextView sixthTxt;
//    @InjectView(R.id.txt_seventh)
//    TextView seventhTxt;
    @InjectView(R.id.list_topic_and_coupon)
    ListView topicAndCouponList;
    @InjectView(R.id.txt_connect_or_public)
    TextView connectOrPublicTxt;
    @InjectView(R.id.txt_null_data)
    TextView nullDataTxt;
    TopicAndCouponAdapter topicAndCouponAdapter;
    List<CouponModel> couponModelList;
    List<Topics> topicsList;
    ScrollListener scrollListener;
    public static final int MARKET_ITEM_COUNT = 4;
    int firstId;
    int secondId;
    int thirdId;
    int fourthId;
//    int fifthId;
//    int sixthId;
//    int seventhId;
    String firstTitle;
    String secondTitle;
    String thirdTitle;
    String fourthTitle;
//    String fifthTitle;
//    String sixthTitle;
//    String seventhTitle;
    String title;
    private int topicSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_market);
        getExtra();
        initView();
        loadData();
    }

    private void getExtra() {
        title = getIntent().getStringExtra(OtherTopicsActivity.TITLE);
    }

    private void loadData() {
        loadIndustryGroup();
        loadMarketTopics();
    }

    private void loadHotCoupon() {
        appAction.listHotCoupon(20, scrollListener.getCurrentPage(), new CallbackListener<List<CouponModel>>() {
            @Override
            public void onSuccess(List<CouponModel> data) {//数据为空判断，以及结束加载判断，还有分页加载策略，通通按照这个模板
                Log.e("tAndc获取最热的券成功", data.size() + "");
                if (scrollListener.getCurrentPage() == 1 && data.size() == 0 && topicsList.size() == 0) {
                    nullDataTxt.setVisibility(View.VISIBLE);
                } else {
                    if (couponModelList.size() == 0) {
                        couponModelList.clear();
                        couponModelList.addAll(data);
                        topicAndCouponAdapter.notifyDataSetChanged();
                    } else {
                        if (data.size() > 0) {
                            couponModelList.addAll(data);
                            topicAndCouponAdapter.notifyDataSetChanged();
                            scrollListener.setLoadFinish(true);
                        }
                    }
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                Log.e("+++++获取最热的券失败", message);
                scrollListener.setLoadFinish(true);
            }
        });
    }

    private void loadIndustryGroup() {
        appAction.queryIndustryGroup(new CallbackListener<List<IndustryGroupModel>>() {
            @Override
            public void onSuccess(List<IndustryGroupModel> data) {
                Log.e("tAndc获取行业成功了", data.size() + "");
                int size = data.size();
                IndustryGroupModel industryGroupModel = new IndustryGroupModel();
                if (size >= MARKET_ITEM_COUNT) {
                    for (int i = 0; i < MARKET_ITEM_COUNT; i++) {
                        industryGroupModel = data.get(i);
                        if (i == 0) {
                            firstTxt.setText(industryGroupModel.getName());
                            firstId = industryGroupModel.getId();
                            firstTitle = industryGroupModel.getName();
                        }
                        if (i == 1) {
                            secondTxt.setText(industryGroupModel.getName());
                            secondId = industryGroupModel.getId();
                            secondTitle = industryGroupModel.getName();
                        }
                        if (i == 2) {
                            thirdTxt.setText(industryGroupModel.getName());
                            thirdId = industryGroupModel.getId();
                            thirdTitle = industryGroupModel.getName();
                        }
                        if (i == 3) {
                            fourthTxt.setText(industryGroupModel.getName());
                            fourthId = industryGroupModel.getId();
                            fourthTitle = industryGroupModel.getName();
                        }
//                        if (i == 4) {
//                            fifthTxt.setText(industryGroupModel.getName());
//                            fifthId = industryGroupModel.getId();
//                            fifthTitle = industryGroupModel.getName();
//                        }
//                        if (i == 5) {
//                            sixthTxt.setText(industryGroupModel.getName());
//                            sixthId = industryGroupModel.getId();
//                            sixthTitle = industryGroupModel.getName();
//                        }
//                        if (i == 6) {
//                            seventhTxt.setText(industryGroupModel.getName());
//                            seventhId = industryGroupModel.getId();
//                            seventhTitle = industryGroupModel.getName();
//                        }
                    }
                } else {
                    firstTxt.setClickable(false);
                    secondTxt.setClickable(false);
                    thirdTxt.setClickable(false);
                    fourthTxt.setClickable(false);
//                    fifthTxt.setClickable(false);
//                    sixthTxt.setClickable(false);
//                    seventhTxt.setClickable(false);
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                Log.e("++++++获取行业失败了", message);
            }
        });
    }

    private void loadMarketTopics() {
        appAction.getTopics(1, PAGE_SIZE, OtherTopicsActivity.MARKET, new CallbackListener<List<Topics>>() {
            @Override
            public void onSuccess(List<Topics> data) {
                Log.e("tAndc获取市场专题成功", data.size() + "");
                topicSize = data.size();
                topicsList.clear();
                topicsList.addAll(data);
                scrollListener = new ScrollListener(topicAndCouponList, true, topicSize, new LoadData() {
                    @Override
                    public void loadMyData() {
                        loadHotCoupon();
                    }
                });
                topicAndCouponList.setOnScrollListener(scrollListener);
                loadHotCoupon();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                Log.e("++++失败了吗", message);
                ToastUtil.showShort(MarketActivity.this, "获取专题失败");
            }
        });
    }

    private void initView() {
        firstTxt.setOnClickListener(this);
        secondTxt.setOnClickListener(this);
        thirdTxt.setOnClickListener(this);
        fourthTxt.setOnClickListener(this);
//        fifthTxt.setOnClickListener(this);
//        sixthTxt.setOnClickListener(this);
//        seventhTxt.setOnClickListener(this);
        backIbtn.setOnClickListener(this);
//        provideForFreeTxt.setOnClickListener(this);
        topicAndCouponList.setOnItemClickListener(this);
        topicAndCouponList.setDivider(null);
        topicsList = new ArrayList<>();
        couponModelList = new ArrayList<>();
        topicAndCouponAdapter = new TopicAndCouponAdapter(MarketActivity.this, topicsList, couponModelList);
        topicAndCouponList.setAdapter(topicAndCouponAdapter);
        connectOrPublicTxt.setText(title);
    }

    @Override
    public void onClick(View view) {
        if (view == backIbtn) {
            finish();
        }
//        if (view == provideForFreeTxt) {
//            if (!TextUtils.isEmpty(UserInfoKeeper.readPhoneNumber(this))) {
//                Intent intent = new Intent(MarketActivity.this, WebViewActivity.class);
//                intent.putExtra("h5Url", WebViewActivity.SHAKE);
//                startActivity(intent);
//            } else {
//                startActivity(new Intent(this, LoginActivity.class));
//            }
//        }
        if (view == firstTxt) {
            Intent intent = new Intent(MarketActivity.this, SortCouponModelActivity.class);
            intent.putExtra(SortCouponModelActivity.INDUSTRY_ID, firstId);
            intent.putExtra(SortCouponModelActivity.INDUSTRY_NAME, firstTitle);
            startActivity(intent);
        }
        if (view == secondTxt) {
            Intent intent = new Intent(MarketActivity.this, SortCouponModelActivity.class);
            intent.putExtra(SortCouponModelActivity.INDUSTRY_ID, secondId);
            intent.putExtra(SortCouponModelActivity.INDUSTRY_NAME, secondTitle);
            startActivity(intent);
        }
        if (view == thirdTxt) {
            Intent intent = new Intent(MarketActivity.this, SortCouponModelActivity.class);
            intent.putExtra(SortCouponModelActivity.INDUSTRY_ID, thirdId);
            intent.putExtra(SortCouponModelActivity.INDUSTRY_NAME, thirdTitle);
            startActivity(intent);
        }
        if (view == fourthTxt) {
            Intent intent = new Intent(MarketActivity.this, SortCouponModelActivity.class);
            intent.putExtra(SortCouponModelActivity.INDUSTRY_ID, fourthId);
            intent.putExtra(SortCouponModelActivity.INDUSTRY_NAME, fourthTitle);
            startActivity(intent);
        }
//        if (view == fifthTxt) {
//            Intent intent = new Intent(MarketActivity.this, SortCouponModelActivity.class);
//            intent.putExtra(SortCouponModelActivity.INDUSTRY_ID, fifthId);
//            intent.putExtra(SortCouponModelActivity.INDUSTRY_NAME, fifthTitle);
//            startActivity(intent);
//        }
//        if (view == sixthTxt) {
//            Intent intent = new Intent(MarketActivity.this, SortCouponModelActivity.class);
//            intent.putExtra(SortCouponModelActivity.INDUSTRY_ID, sixthId);
//            intent.putExtra(SortCouponModelActivity.INDUSTRY_NAME, sixthTitle);
//            startActivity(intent);
//        }
//        if (view == seventhTxt) {
//            Intent intent = new Intent(MarketActivity.this, SortCouponModelActivity.class);
//            intent.putExtra(SortCouponModelActivity.INDUSTRY_ID, seventhId);
//            intent.putExtra(SortCouponModelActivity.INDUSTRY_NAME, seventhTitle);
//            startActivity(intent);
//        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Log.e("tAndc", i+"");
        if (i < topicsList.size()) {
            Topics topics = topicsList.get(i);
            appAction.behavior(topics.getUuid(), UserInfoKeeper.readCustomerId(MarketActivity.this), OtherTopicsActivity.BROWSE, new CallbackListener<Void>() {
                @Override
                public void onSuccess(Void data) {
                    Log.e("+++浏览成功", "浏览真的成功了");
                }

                @Override
                public void onFailure(String errorCode, String message) {
                    ToastUtil.showShort(MarketActivity.this, message);
                }
            });
            Intent intent = new Intent(MarketActivity.this, TicketsListActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable(TicketsListActivity.TOPICS, topics);
            bundle.putString(TicketsListActivity.INDEX, "market");
            intent.putExtra(TicketsListActivity.BUNDLE_TOPICS, bundle);
            startActivity(intent);
        } else if(i > topicsList.size()){
            CouponModel couponModel = couponModelList.get(i - topicsList.size() - 1);//“推荐货品”的影响所以要减1
            Intent intent = new Intent(MarketActivity.this, CommodityDetailsActivity.class);
            intent.putExtra(CommodityDetailsActivity.COUPONMODEL_ID, couponModel.getId());
            startActivity(intent);

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("商城首页");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("商城首页");
        MobclickAgent.onPause(this);
    }

    @Override
    public void finish() {
        Intent intent = new Intent(this,MainTabActivity.class);
        intent.putExtra(MainTabActivity.INDEX, MainTabActivity.TAG_INDEX);
        startActivity(intent);
    }
}
